package pt.ipleiria.plp.ui;

import de.enough.polish.ui.Alert;
import de.enough.polish.ui.AlertType;
import de.enough.polish.ui.ChoiceItem;
import de.enough.polish.ui.Command;
import de.enough.polish.ui.Displayable;
import de.enough.polish.util.Locale;

import java.util.Enumeration;
import javax.microedition.lcdui.Image;
import pt.ipleiria.plp.BlackSpotterMidlet;
import pt.ipleiria.plp.threads.CallbackConfirm;
import pt.ipleiria.plp.Lixeiras.LixeiraPeddingComplete;
import pt.ipleiria.plp.Lixeiras.LixeiraSent;
import pt.ipleiria.plp.threads.DeleteLixeiraFromServer;
import pt.ipleiria.plp.threads.SendLixeira;

/**
 *
 * @author Ricardo
 */
public class MeusEventosScreen extends VerEventosScreen implements CallbackConfirm {

    OnHold hold;

    public MeusEventosScreen(String title, int listType, BlackSpotterMidlet midlet) {
        super(title, listType, midlet);

        populateList();
    }

    public MeusEventosScreen(int listType, String[] stringElements, Image[] imageElements, BlackSpotterMidlet midlet) {
        super(Locale.get("title.NovaLixeira"), listType, stringElements, imageElements, midlet);

        populateList();
    }

    public void populateList() {
        if (this.size() != 0) {
            this.deleteAll();
        }
        Enumeration enume = getMidlet().getApp().getManager().getLixeirasEnviadas();
        if (enume != null) {
            while (enume.hasMoreElements()) {

                LixeiraSent temp = ((LixeiraSent) enume.nextElement());
                //#style mainItem
                ItemLix itl = new ItemLix(temp.getTitle(), temp.getGuid(), true,
                        null, ChoiceItem.HORIZONTAL & ChoiceItem.LAYOUT_CENTER);

                int index = this.append(itl);
                this.getItem(index).addCommand(getCmdDel());
                this.getItem(index).addCommand(getCmdEdit());

            }
        }
        enume = getMidlet().getApp().getManager().getLixeirasPorEnviar();
        if (enume != null) {

            while (enume.hasMoreElements()) {

                LixeiraPeddingComplete temp = ((LixeiraPeddingComplete) enume.nextElement());
                //#style mainItem
                ItemLix itl = new ItemLix(temp.getTitle(), temp.getGuid(), false,
                        null, ChoiceItem.HORIZONTAL & ChoiceItem.LAYOUT_CENTER);

                int index = this.append(itl);
                this.getItem(index).addCommand(getCmdDel());
                this.getItem(index).addCommand(getCmdEdit());
                //Only registred users can send
                if (getMidlet().getApp().getAppKey() != null) {
                    this.getItem(index).addCommand(getCmdEnviar());
                }
            }
        }
    }

    public void selectListItem() {
    }

    public void commandAction(Command c, Displayable d) {
        if (c == getCmdEdit()) {
            int selectedIndex = this.getSelectedIndex();
            ItemLix temp = (ItemLix) this.getItem(selectedIndex);
            getMidlet().editLixeira(temp.getGuid(), temp.isEnviada());

        } else if (c == getCmdMenu()) {
            getMidlet().getDisplay().setCurrent(getMidlet().getMainMenu());
        } else if (c == getCmdEnviar()) {

            int selectedIndex = this.getSelectedIndex();
            LixeiraPeddingComplete lix = getMidlet().getApp().getManager().
                    searchLixeirasPorEnviadar(((ItemLix) this.getItem(selectedIndex)).getGuid());
            System.out.println("GUID: " + lix.getGuid());
            SendLixeira t = new SendLixeira(lix, this, getMidlet());
            t.start();

            hold = new OnHold(getMidlet(), this);
            getMidlet().getDisplay().setCurrent(hold);

        } else if (c == getCmdDel()) {

            int selectedIndex = this.getSelectedIndex();
            ItemLix temp = (ItemLix) this.getItem(selectedIndex);
            DeleteLixeiraFromServer t = new DeleteLixeiraFromServer(temp.getGuid(),
                    temp.isEnviada(), getMidlet(), this);
            hold = new OnHold(getMidlet(), this);
            getMidlet().getDisplay().setCurrent(hold);
            t.start();


        } else {
            super.commandAction(c, d);
        }
    }

    public void returnConfimation(String error) {
        populateList();
        if (error == null) {

            Alert alert = new Alert(Locale.get("title.sucesso"),
                    Locale.get("mensagens.sucesso"), null, AlertType.INFO);
            getMidlet().getDisplay().setCurrent(alert, this);

        } else {

            Alert alert = new Alert(Locale.get("title.Erro"),
                    error, null, AlertType.ERROR);
            getMidlet().getDisplay().setCurrent(alert, this);

        }

    }
}
